package com.cnunicom.modules.business.service.impl;

import com.cnunicom.modules.addressbook.entity.AddressBookEntity;
import com.cnunicom.modules.business.entity.BusinessDeptEntity;
import com.cnunicom.modules.business.service.BusinessDeptService;
import com.cnunicom.modules.sys.entity.SysUserEntity;
import com.mchange.lang.LongUtils;
import io.netty.util.internal.StringUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cnunicom.common.utils.PageUtils;
import com.cnunicom.common.utils.Query;

import com.cnunicom.modules.business.dao.BusinessWorkDao;
import com.cnunicom.modules.business.entity.BusinessWorkEntity;
import com.cnunicom.modules.business.service.BusinessWorkService;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;


@Service("businessWorkService")
public class BusinessWorkServiceImpl extends ServiceImpl<BusinessWorkDao, BusinessWorkEntity> implements BusinessWorkService {

    @Resource
    private BusinessDeptService businessDeptService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        BusinessWorkEntity entity = getBusinessWorkEntity(params);

        IPage<BusinessWorkEntity> page = this.page(
                new Query<BusinessWorkEntity>().getPage(params),
                new QueryWrapper<BusinessWorkEntity>()
                        .like(StringUtils.isNotBlank(entity.getTitle()),"title",entity.getTitle())
                        .setEntity(entity)
        );
        return new PageUtils(page);
    }



    /**
     * 获取当前选中业务的关联部门,并转换
     * @param id
     * @return
     */
    private List<String> getBusinessDepts(Long id){
        return businessDeptService.list(
                new QueryWrapper<BusinessDeptEntity>().lambda().eq(BusinessDeptEntity::getBusinessId, id))
                .stream().map(BusinessDeptEntity::getName).collect(Collectors.toList());
    }

    /**
     * page查询,参数处理
     * @param params
     * @return
     */
    private BusinessWorkEntity getBusinessWorkEntity(Map<String, Object> params){

        BusinessWorkEntity entity = new BusinessWorkEntity();

        String articleType = (String)params.get("articleType");
        String title = (String)params.get("title");

        SysUserEntity user = (SysUserEntity)params.get("user");
        Long userId = user.getUserId();
        boolean isAuthor = SecurityUtils.getSubject().hasRole("作者");

        if (StringUtils.isNotBlank(articleType)) {
            entity.setArticleType(Integer.parseInt(articleType));
        } else if (isAuthor) {
            entity.setCreateUserId(userId);
        } else if (StringUtils.isNotBlank(title)) {
            entity.setTitle(title);
        }

        return entity;
    }

}
